<template>
  <el-form :model="accountFormData" class="demo-form-inline">
    <el-row justify="end">
      <el-col :span="12">
        <el-form-item label="用户昵称">
          <el-input v-model="accountFormData.nickName" placeholder="请输入用户昵称称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="归属部门">
          <mine-tree-select v-model="accountFormData.deptId" :options="allDept"></mine-tree-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="手机号码">
          <el-input v-model="accountFormData.mobile" placeholder="请输入手机号码"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="邮箱">
          <el-input v-model="accountFormData.email" placeholder="请输入联系邮箱"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="登录账号">
          <el-input v-model="accountFormData.accountName" placeholder="请输入联系邮箱"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="用户密码">
          <el-input v-model="accountFormData.password" placeholder="请输入用户密码"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="用户性别">
          <el-radio v-model="accountFormData.sex" :label="1" size="middle">男</el-radio>
          <el-radio v-model="accountFormData.sex" :label="2" size="middle">女</el-radio>
          <el-radio v-model="accountFormData.sex" :label="0" size="middle">保密</el-radio>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="部门状态">
          <el-radio v-model="accountFormData.status" :label="1" size="middle">正常</el-radio>
          <el-radio v-model="accountFormData.status" :label="0" size="middle">停用</el-radio>
          <el-radio v-model="accountFormData.status" :label="2" size="middle">待激活</el-radio>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="岗位">
          <el-select size="middle" v-model="accountFormData.postId">
            <el-option v-for="post in postList" :key="post.id" :label="post.postName" :value="post.id"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="角色">
          <el-select size="middle" v-model="accountFormData.roleId">
            <el-option v-for="role in roleList" :key="role.id" :label="role.roleName" :value="role.id"/>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>

    <el-form-item label="备注">
      <el-input type="textarea"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {ref, getCurrentInstance, onMounted} from 'vue'
import {save, updateById, allRole, allPost} from '@/api/account/index.js'
import MineTreeSelect from "@/components/select/mine-tree-select";

const {ElMessage} = require("element-plus");

export default {
  name: "mine-account-add",
  components: {MineTreeSelect},
  props: {
    allDept: {
      required: true,
      type: Array
    },
    accountFormData: {
      required: true,
      default: {
        parentId: "0",
        startCreateTime: null,
        endCreateTime: null
      },
      type: Object
    }
  },
  setup(props) {
    const {proxy} = getCurrentInstance()
    const roleList = ref([])
    const postList = ref([])
    const onSubmit = () => {
      if (props.accountFormData.id) {
        updateById(props.accountFormData).then(res => {
          if (res.success) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            proxy.$bus.emit('submitAccountSuccess')
          } else {
            ElMessage({
              message: res.msg,
              type: 'warning',
            })
          }
        })
      } else {
        save(props.accountFormData).then(res => {
          if (res.success) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            proxy.$bus.emit('submitAccountSuccess')
          } else {
            ElMessage({
              message: res.msg,
              type: 'warning',
            })
          }
        })
      }
    }

    const initRole = () => {
      allRole().then(res => {
        if (res.success) {
          roleList.value = res.data
        }
      })
    }

    const initPost = () => {
      allPost().then(res => {
        if (res.success) {
          postList.value = res.data
        }
      })
    }
    onMounted(() => {
      initRole()
      initPost()
    })
    return {
      postList,
      roleList,
      onSubmit
    }
  }
}
</script>

<style scoped>

</style>
